<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Sushitto extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('captcha');
    }

    public function index() {
        $data['error'] = "";
        $data['message'] = $this->session->flashdata('message');
        //redireccion segura
        if ($this->session->userdata('access')) {
            redirect('xpanel');
        }

        $cap = $this->getCaptcha();
        // se agrega el captcha a la base de datos
        $this->evaluacion_model->insertCaptcha($cap['time'], $this->input->ip_address(), $cap['word']);
        $data['cap'] = $cap;
        $this->load->view('login/login_view', $data);
    }

    public function x() {
        # SET VALIDATION RULES
        $this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_captcha_check');
        $this->form_validation->set_rules('user', 'username', 'required|trim|xss_clean|min_length[6]|max_length[15]');
        $this->form_validation->set_rules('pass', 'password', 'required|trim|xss_clean|min_length[6]|max_length[20]');

        # has the form been submitted and with valid form info (not empty values)
        if (!$this->form_validation->run()) {
            $this->session->set_flashdata('message', 'Datos incorrectos. Vuelve a intentarlo');
            redirect('sushitto');
        }

        # autenticacion segura
        $usuario = $this->security->xss_clean($this->input->post('user'));
        $pass = $this->security->xss_clean($this->input->post('pass'));
        $captcha = $this->security->xss_clean($this->input->post('captcha'));

        if ($this->verifica($usuario, $pass, $captcha)) {
            $this->evaluacion_model->deleteCaptcha($this->input->ip_address());
            $admin = $this->session->userdata('admin');
            switch ($admin['ta_tipo_administrativo_id']) {
                #supervisor
                case 1:
                    redirect('xpanel');
                    break;
                #responsable
                case 2:
                    redirect('rpanel');
                    break;
                #monitor
                case 3:
                    redirect('mpanel');
                    break;
                #franquicionario
                case 4:
                    redirect('fpanel');
                    break;
            }
        } else {
        $data['error'] = "Datos incorrectos. Vuelve a intentarlo";
        $data['cap'] = $this->getCaptcha();
        $data['message'] = '';
        $this->load->view('login/login_view', $data);
        }
    }

    function captcha_check() {
        $expiration = time() - 7200; // Limite de dos horas
        $binds = array($this->input->post('captcha'), $this->input->ip_address(), $expiration);
        return $this->evaluacion_model->captchaExist($binds);
    }

    private function verifica($usuario, $pass, $captcha) {
        $result = $this->evaluacion_model->verificar_datos($usuario, $pass, $captcha);
        #existe usuario
        if (is_object($result)) {
            $admin['a_administrativo_id'] = $result->a_administrativo_id;
            $admin['a_nombre'] = $result->a_nombre;
            $admin['a_email'] = $result->a_email;
            $admin['ta_tipo_administrativo_id'] = $result->ta_tipo_administrativo_id;
            $admin['ta_tipo'] = $result->ta_tipo;
            $admin['a_imagen'] = $result->a_imagen;
            $this->session->set_userdata('admin', $admin);
            return true;
        }
        # datos no existentes
        return false;
    }

    public function getCaptcha(){
        $vals = array(
            'img_path' => './assets/captcha/',
            'img_url' => base_url() . "assets/captcha/",
            'font_path' => './assets/captcha/fonts/OpenSans-Regular.ttf',
            'img_width' => '250',
            'img_height' => 30,
            'expiration' => 7200
        );
        return create_captcha($vals);
    }

}
